package com.example.demo.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.mapper.SmsMapper;
import com.example.demo.utils.smsUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.demo.service.ISmsService;
import com.example.demo.entity.Sms;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 残月
 * @since 2023-02-25
 */
@RestController
@RequestMapping("/sms")
public class SmsController {

    @Resource
    private ISmsService smsService;

    @Resource
    private SmsMapper smsMapper;


    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Sms sms) {
        return Result.success(smsService.saveOrUpdate(sms));
    }

    @GetMapping("/sendSms")
    public Result sendSms(@RequestParam("phone") String phone) {
        return Result.success(smsUtil.sendSms(phone));
    }

    @PostMapping("/batchSendSms")
    public Result batchSendSms(@RequestParam("phone") List<String> phone) {
        return Result.success(smsUtil.batchSendSms(phone));
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(smsService.removeById(id));
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(smsService.removeByIds(ids));
    }

    @GetMapping
    public Result findAll() {
        return Result.success(smsService.list());
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {

        List<Map<String, Object>> list = CollUtil.newArrayList();

        List<Sms> all = smsMapper.selectList(null);
        for (Sms sms : all) {
            Map<String, Object> row = new LinkedHashMap<>();
            row.put("序号", sms.getId());
            row.put("用户名", sms.getName());
            row.put("电话号码", sms.getPhone());
            list.add(row);
        }

        // 2. 写excel
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(list, true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(System.out);
    }
    @PostMapping("/import")
    public Result upload(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        List<List<Object>> lists = ExcelUtil.getReader(inputStream).read(1);
        List<Sms> saveList = new ArrayList<>();
        for (List<Object> row : lists) {
            Sms sms = new Sms();
            sms.setName(row.get(1).toString());
            sms.setPhone(row.get(2).toString());
            saveList.add(sms);
        }
        for (Sms sms : saveList) {
            smsMapper.insert(sms);
        }
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(smsService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam String name) {
        QueryWrapper<Sms> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        queryWrapper.orderByDesc("id");
        return Result.success(smsService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

